package com.ita.acmsystem.merchant.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ita.acmsystem.merchant.service.IFoodService;
import com.ita.acmsystem.merchant.service.impl.FoodServiceImpl;

/**
 * Servlet implementation class AjaxDeleteFoodServlet
 */
public class AjaxDeleteFoodServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private IFoodService foodService = new FoodServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AjaxDeleteFoodServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		deleteFood(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		deleteFood(request, response);
	}

	private void deleteFood(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String foodId = request.getParameter("foodId");
		PrintWriter writer = response.getWriter();
		if (foodService.deleteFoods(foodId)) {
			// response.sendRedirect("ContentServlet");
		} else {
			writer.write("<h1>Delete Fail!</h1>");
		}
		writer.close();
	}

}
